#include "stdafx.h"
#include "pgl/mfc/PGLAddPointEvent.h"
#include "TestPGL/PGLLineContainer.h"
#include "pgl/shape/PGLGeoLine.h"

void CPGLAddPointEvent::RemoveObject(long id)
{
	CPGLRegion* pRegion=GetRegion().get();
	CPGLObjectManager mgr=pRegion->GetObjectManager();
	int size=mgr.GetCount();
	for(int i=0;i<size;i++)
	{
		if(mgr[i]->GetID()==id)
		{
			pRegion->DeleteObject(i);
			Refresh();
			break;
		}
	}
}
CPGLObjectPtr CPGLAddPointEvent::GetObject(long id)
{
	CPGLObjectManager mgr=GetRegion().get()->GetObjectManager();
	int size=mgr.GetCount();
	for(int i=0;i<size;i++)
	{
		if(mgr[i]->GetID()==id)
		{
			return CPGLObjectPtr(mgr[i]);
		}
	}
	return CPGLObjectPtr();
}
CPGLGeoPointDPtr CPGLAddPointEvent::AddPoint(CPoint pt)
{
	double x,y;
	GetCoordPoint(pt,x,y);
	CPGLGeoPointDPtr point(new CPGLGeoPointD(x,y));
	point->SetSolidFillColor(0,0,0);
	GetRegion()->AddObject(point);
	Refresh();
	return point;
}
void CPGLAddPointEvent::OnLButtonDown( UINT nFlags, CPoint point )
{
	//CPGLGeoPointDPtr pt=AddPoint(point);
	double x,y;
	GetCoordPoint(point,x,y);
	CPGLGeoPointDPtr pt(new CPGLGeoPointD(x,y));
	m_curline->AddPoint(pt.get());
	Refresh();
};
void CPGLAddPointEvent::OnMouseMove( UINT nFlags, CPoint point )
{
	double x,y;
	GetCoordPoint(point,x,y);
	m_tVx[1]=static_cast<float>(x);
	m_tVy[1]=static_cast<float>(y);
	CPGLRegion* pRegion=GetRegion().get();
	RemoveObject(m_tid);
	
	CPGLGeoLineDPtr line(new CPGLGeoLineD);
	m_tid=line->GetID();
	line->AddPoint(CPGLGeoPointDPtr(new CPGLGeoPointD(x,y)).get());
	pRegion->AddObject(line);
	Refresh();
}
void CPGLAddPointEvent::OnLButtonDblClk(UINT nFlags,CPoint point)
{
	m_curline=CPGLGeoLineDPtr(new CPGLGeoLineD);
	RemoveObject(m_tid);
}
long CPGLAddPointEvent::m_id=-1;
long CPGLAddPointEvent::m_tid=-1;
